package eerbaar.v2.app.mover;

import eerbaar.game.IGame;
import eerbaar.message.lib.NotifyPetMove;
import eerbaar.persona.IPersona;
import eerbaar.position.WrongPositionException;
import eerbaar.shared.message.MsgHelper;
import eerbaar.v2.app.GameHelper;
import eerbaar.v2.app.strategy.FreePlayStrategy;
import eerbaar.v2.app.strategy.GoToStrategy;
import eerbaar.v2.app.strategy.IPersonaStrategy;
import eerbaar.v2.app.strategy.ProgramStrategy;
import eerbaar.v2.app.strategy.SitStrategy;
import eerbaar.v2.app.strategy.WaitStrategy;

public class PersonaMover implements IPersonaMover {

    private IPersona subject;
    private IPersonaStrategy strategy;

    public PersonaMover(IPersona subject) {
        this.subject = subject;
        sit();
    }

    @Override
    public void disconnect() {

        this.subject = null;
    }

    @Override
    public boolean equals(Object obj) {

        IPersona otherP = null;
        if (obj instanceof IPersona) {
            otherP = (IPersona) obj;
        }
        if (obj instanceof PersonaMover) {
            PersonaMover other = (PersonaMover) obj;
            otherP = other.getPersona();
        }
        if (otherP == null) {
            return false;
        }
        return (otherP == this.subject);
    }

    @Override
    public void freePlay() {

        this.strategy = new FreePlayStrategy(this.subject);
    }

    public IPersona getPersona() {
        return this.subject;
    }

    @Override
    public IPersonaStrategy getStrategy() {

        return this.strategy;
    }

    @Override
    public void moveTo(int x, int y) {

        this.strategy = new GoToStrategy(this.subject, x, y);
    }

    @Override
    public void pleaseWait() {

        this.strategy = new WaitStrategy();
    }

    @Override
    public void runProgram(String command) {

        this.strategy = new ProgramStrategy(this.subject, command);
    }

    public void sendNotify() {

        if (this.subject.getName().equals("pet")) {
            MsgHelper.sendMsg(NotifyPetMove.class, new NotifyPetMove());
        }
    }

    @Override
    public void sit() {

        this.strategy = new SitStrategy(this.subject);
    }

    @Override
    public void triggerMove() {

        IGame game = GameHelper.getGame();

        if (game == null) {
            return;
        }
        if (this.strategy == null) {
            return;
        }

        boolean wasMoved = false;
        try {
            if (this.strategy.move(this, game)) {

                wasMoved = true;
                sendNotify();
            }
        } catch (WrongPositionException e) {
            // ignore
        }

        if (this.subject.getName().equals("pet")) {
            updateState(game, wasMoved);
        }
    }

    private void updateState(IGame game, boolean wasMoved) {

        IPersona persona = this.subject;
        if (persona == null) {
            return;
        }

        if (wasMoved) {

            persona.setState(IPersona.State.ACTIVE);
        } else {

            if (persona.getState() == null
                    || !persona.getState().equals(IPersona.State.REST)) {

                persona.setState(IPersona.State.REST);
            }
        }
    }

    @Override
    public void waitForStart() {

        this.sit();

        if (this.subject.getProgram() != null) {
            new PetWatcher(this, this.subject.getProgram());
        }
    }

}
